BrewServices.link   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 7
c 0
b 0
f 0
rs 10
cc 2
1
import execa from 'execa'
2
import ServiceCtl from '../serviceCtl'
3
4
class BrewServices extends ServiceCtl {
5
    private static instance: BrewServices;
6
7
    alias: string
8
    name: string
9
    path: string
10
11
    private constructor() {
12
        super()
13
14
        let usrLocalDir
15
        switch (`${process.platform}_${process.arch}`) {
16
        case 'darwin_arm64':
17
            usrLocalDir = '/opt/homebrew'
18
            break
19
        default: // darwin x64
20
            usrLocalDir = '/usr/local'
21
            break
22
        }
23
24
        this.alias = 'brew'
25
        this.name = 'Homebrew'
26
        this.path = `${usrLocalDir}/bin/brew`
27
    }
28
29
    public static getInstance(): ServiceCtl {
30
        if (!BrewServices.instance) {
31
            BrewServices.instance = new BrewServices()
32
        }
33
34
        return BrewServices.instance
35
    }
36
37
    async reload(pkg: string): Promise<boolean> {
38
        try {
39
            await execa('brew', ['services', 'reload', pkg], {shell: true})
40
            return true
41
        } catch (e) {
42
            return false
43
        }
44
    }
45
46
    async restart(pkg: string): Promise<boolean> {
47
        try {
48
            await execa('brew', ['services', 'restart', pkg], {shell: true})
49
            return true
50
        } catch (e) {
51
            return false
52
        }
53
    }
54
55
    async start(pkg: string): Promise<boolean> {
56
        await execa('brew', ['services', 'start', pkg], {shell: true})
57
        return true
58
    }
59
60
    async stop(pkg: string): Promise<boolean> {
61
        try {
62
            await execa('brew', ['services', 'stop', pkg], {shell: true})
63
            return true
64
        } catch (e) {
65
            return false
66
        }
67
    }
68
69
    async reloadAsRoot(pkg: string): Promise<boolean> {
70
        try {
71
            await execa('sudo', ['brew', 'services', 'reload', pkg], {shell: true})
72
            return true
73
        } catch (e) {
74
            return false
75
        }
76
    }
77
78
    async restartAsRoot(pkg: string): Promise<boolean> {
79
        try {
80
            await execa('sudo', ['brew', 'services', 'restart', pkg], {shell: true})
81
            return true
82
        } catch (e) {
83
            return false
84
        }
85
    }
86
87
    async startAsRoot(pkg: string): Promise<boolean> {
88
        try {
89
            await execa('sudo', ['brew', 'services', 'start', pkg], {shell: true})
90
            return true
91
        } catch (e) {
92
            return false
93
        }
94
    }
95
96
    async stopAsRoot(pkg: string): Promise<boolean> {
97
        try {
98
            await execa('sudo', ['brew', 'services', 'stop', pkg], {shell: true})
99
            return true
100
        } catch (e) {
101
            return false
102
        }
103
    }
104
105
    async link(pkg: string): Promise<boolean> {
106
        try {
107
            await execa('brew', ['link', '--overwrite', '--force', pkg], {shell: true})
108
            return true
109
        } catch (e) {
110
            return false
111
        }
112
    }
113
114
    async unlink(pkg: string): Promise<boolean> {
115
        try {
116
            await execa('brew', ['unlink', pkg], {shell: true})
117
            return true
118
        } catch (e) {
119
            return false
120
        }
121
    }
122
}
123
124
export default BrewServices
125